package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author ：MJS
 * @date ：Created in 2021/5/13  18:47
 * @description：TODO
 */
@RestController
@Api(description = "商品详情微服务")
@RequestMapping("/api/item")
public class ItemApiController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;
    //数据汇总  页面所需要的  4部分数据
    @GetMapping("/getItem/{skuId}")
    public Map<String,Object> getItem(@PathVariable Long skuId){
        Map<String,Object>result=new HashMap<>();
        //1.skuInfo  库存消息，  skuinfo图片
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);
        //2.一二三级分类
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);
        //3.销售属形及值及选中红边框
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrValueChecked = productFeignClient.getSpuSaleAttrValueChecked(skuInfo.getId(), skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrValueChecked);
        });

        //4.销售属性值组合对应库存ID  入参：商品id spuid
        CompletableFuture<Void> valuesSkuJson1CompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map valueSkuJson = productFeignClient.getValueSkuJson(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(valueSkuJson));
        });

        //5.查询价格  不将价格放在缓存之中
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        //6.查询热度
        CompletableFuture.runAsync(()->{
             listFeignClient.hostScore(skuId);
        },threadPoolExecutor);
        //等待所有多线程执行完成  可以不用等第一个 因为2.3.4完成的前提是1得完成 6也可以不用等
        CompletableFuture.allOf(categoryViewCompletableFuture,
                spuSaleAttrListCompletableFuture,
                valuesSkuJson1CompletableFuture,
                priceCompletableFuture).join();
        return result;
    }
}
